package com.cuixichang.minimalism.core.security.core;

import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.cuixichang.minimalism.basal.base.Result;
import com.cuixichang.minimalism.basal.enums.ResponseCoderEnum;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DefaultResponseChain implements ResponseChain {
    private static final Log logfactory = LogFactory.get();

    @Override
    public boolean matching(Exception e) {
        return true;
    }

    @Override
    public void invoke(HttpServletResponse httpServletResponse, Exception e) throws IOException {
        httpServletResponse.setStatus(HttpStatus.OK.value());
        logfactory.error(e);
        Result result = Result.error(ResponseCoderEnum.Failed.getCode(),
                ResponseCoderEnum.Failed.getDesc());
        httpServletResponse.getWriter().write(JSONUtil.toJsonStr(result));
    }
}
